$(function() {
	/*外页上传*/
	var $list = $('#fileList_pre'),

	ratio = window.devicePixelRatio || 1,
	thumbnailWidth = 100 * ratio,
	thumbnailHeight = 100 * ratio,
	uploader,uploader_pre_prcture_inn;
	var web_path = document.getElementById("web_path").value;
	var type = document.getElementById("type").value;
	uploader = WebUploader.create({
		auto: true,
		swf: 'lib/webuploader/0.1.5/Uploader.swf',
		server: web_path+'/backstage/common/uploadPic',
		pick: '#filePicker_pre',
		
		formData : {
			foldername : 'resource'
		}
	});

	uploader.on( 'fileQueued', function( file ) {
		if('2' == type){
			var $li = $(
				'<div id="' + file.id + '" class="file-item thumbnail">' +
				'<img>' +
				'<div class="info">' + file.name + '</div>' +
				'</div>'
				),
			$img = $li.find('img');
	
			$list.append( $li );
			uploader.makeThumb( file, function( error, src ) {
				if ( error ) {
					$img.replaceWith('<span>不能预览</span>');
					return;
				}
					$('#pre_img').attr( 'src', src );
					$img.attr( 'src', src );
			}, thumbnailWidth, thumbnailHeight );
		}
	});

	uploader.on( 'uploadSuccess', function(file, response) {
		$( '#'+file.id ).addClass('upload-state-done');
		$("#res_image").val(response.datas);

		if('1' == type){
			$('#show_type').append("<audio controls='controls'><source src='"+response.datas+"' type='audio/mpeg'></audio>");
			
		}
		
		
	});

	uploader.on( 'uploadError', function( file ) {
		var $li = $( '#'+file.id ),
		$error = $li.find('div.error');
		if ( !$error.length ) {
			$error = $('<div class="error"></div>').appendTo( $li );
		}

		$error.text('上传失败');
	});

	uploader.on( 'uploadComplete', function( file ) {
		$( '#'+file.id ).find('.progress').remove();
	});
});
